package com.ruoyi.mobile.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.ruoyi.common.core.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TencentUtils {
    private static final Logger logger = LoggerFactory.getLogger(TencentUtils.class);
    private String endpoint = "http://oss-cn-guangzhou.aliyuncs.com";
    // 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建。
    private String accessKeyId = "LTAI5tK3Y2uKk8xQLn5vanLp";
    private String accessKeySecret = "Yl1XWYJzSR7QkXZMai1UKNibsUuSMd";
    private String bucketName = "guofangoss";

    private static TencentUtils instance;
    // 1 初始化用户身份信息(secretId, secretKey)
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

    public static TencentUtils getInstance() {
        if (instance == null) {
            instance = new TencentUtils();
        }
        return instance;
    }

    /**
     * 上传到COS服务器 如果同名文件会覆盖服务器上的
     * @param instream 文件流
     * @param fileName 文件名称 包括后缀名
     * @return 出错返回"" ,唯一MD5数字签名
     */
    public List<Map<String, String>> uploadFile2Cos(InputStream instream, String fileName) {
        List<Map<String, String>> fileNameList = new ArrayList<>();
        Map<String, String> map = new HashMap<>();
        ossClient.putObject(bucketName, fileName, instream);
        map.put(bucketName, fileName);
        fileNameList.add(map);
        return fileNameList;
    }

    /**
     * @return String 阿里云文件URL
     */
    public String uploadfile(File file) {
        String originalFilename = file.getAbsolutePath();
        String substring = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase();
        LocalDate date = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        // 指定要上传到 COS 上的路径
        String fileName = (System.currentTimeMillis() + substring);// 保存文件的名字命名方式
        try {
            InputStream inputStream = new FileInputStream(file);
            fileName = "voice/" + formatter.format(date) + "/" + fileName;
            ossClient.putObject(bucketName, fileName, inputStream);
            file.delete();
            return "https://guofangoss.oss-cn-guangzhou.aliyuncs.com/" + fileName;
        } catch (FileNotFoundException e) {
            return null;
        }
    }

    /**
     * 删除阿里云上文件对象
     * @param key 阿里云文件URL
     */
    public void del(String key){
        // 指定要删除的 bucket 和对象键
        ossClient.deleteObject(bucketName, key);
    }

    public static String getFileSuffixName(String fileName) {
        String suffixName = null;
        if (!StringUtils.isEmpty(fileName)) {
            suffixName = fileName.substring(fileName.lastIndexOf(".") + 1);
        }
        return suffixName;
    }

    public Long getObjectSize(String key) {
        InputStream objectContent = ossClient.getObject(bucketName, key).getObjectContent();
        long size = 0;
        try {
            size = objectContent.available();
        }catch (IOException ex) {}
        return Long.valueOf(size);
    }
}
